
//
//  KindOfViewController.m
//  DolphinMusic
//
//  Created by dllo on 15/3/26.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "KindOfViewController.h"
#import "UIColor+AddColor.h"
#import "AlbumTableViewCell.h"
#import "AlbumViewController.h"
#import "FMAlbum.h"
#import "UIScrollView+MJRefresh.h"
#import "MJRefresh.h"

@interface KindOfViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *albumArr;
@property (nonatomic, retain)MBProgressHUD *HUD;
@property (nonatomic, retain)NSString *nextPageAddress;
@property (nonatomic, assign)NSInteger page;

@end

@implementation KindOfViewController

- (void)dealloc
{
    [_nextPageAddress release];
    [_HUD release];
    [_albumArr release];
    [_tableView release];
    [_myTitle release];
    [_address release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 20)];
    view1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view1];
    [view1 release];
    
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, VIEW_WIDTH, 44)];
    UIImage *image1 = [UIImage imageNamed:@"bg2.jpg"];
    image1 = [image1 resizableImageWithCapInsets:UIEdgeInsetsMake(1, 1, 1, 1)];
    imageView1.userInteractionEnabled = YES;
    imageView1.image = image1;
    [self.view addSubview:imageView1];
    [imageView1 release];
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftButton.frame = CGRectMake(0, 0, 50, 44);
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 12, 20, 20)];
    imageView.image = [UIImage imageNamed:@"backButton.png"];
    [leftButton addSubview:imageView];
    [imageView release];
    
    
    [leftButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [imageView1 addSubview:leftButton];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 20, VIEW_WIDTH - 200, 44)];
    titleLabel.text = self.myTitle;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:titleLabel];
    [titleLabel release];

    
    self.page = 1;
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, VIEW_WIDTH, VIEW_HEIGHT - 64 - (70.0 / 667) * VIEW_HEIGHT)  style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor colorFromHexCode:@"#afb4db"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.rowHeight = (VIEW_HEIGHT - 64) / 5;
    self.tableView.separatorColor = [UIColor clearColor];
    
    [self.tableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(pullAction)];
    
    [self.tableView registerClass:[AlbumTableViewCell class] forCellReuseIdentifier:@"albumtableviewcellreuse"];
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.HUD = [[MBProgressHUD alloc] initWithView:self.tableView];
    [self.tableView addSubview:self.HUD];
    self.HUD.labelText = @"客官请稍等...";
    self.HUD.dimBackground = YES;
    [self.HUD show:YES];
    [self.HUD release];
    
    
    
    if ([self.type isEqualToString:@"音乐"]) {
        self.address = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_album_list?category_name=music&condition=hot&device=android&page=1&per_page=20&status=0&tag_name=%@", self.myTitle];
        }
    
    
    if ([self.type isEqualToString:@"新闻资讯"]) {
        self.address = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_album_list?category_name=news&condition=hot&device=android&page=1&per_page=20&status=0&tag_name=%@", self.myTitle];
        }
    
    
    [NetHandler getDataWithUrl:self.address completion:^(id dict) {
        
        self.albumArr = [NSMutableArray array];
        
        NSArray *arr = [dict objectForKey:@"list"];
        
        if ([arr isKindOfClass:[NSArray class]]) {
        for (NSDictionary *dic in arr) {
            FMAlbum *album = [[FMAlbum alloc] initWithDictionary:dic];
            [self.albumArr addObject:album];
            [album release];
        }
        [self.tableView reloadData];
        [self.HUD hide:YES];
        [self.HUD removeFromSuperview];
        }
        
    }];
    
}

- (void)pullAction
{
    self.page++;
    
    if ([self.type isEqualToString:@"音乐"]) {
        self.nextPageAddress = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_album_list?category_name=music&condition=hot&device=android&page=%ld&per_page=20&status=0&tag_name=%@", self.page, self.myTitle];
    }
    
    if ([self.type isEqualToString:@"新闻资讯"]) {
        self.nextPageAddress = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_album_list?category_name=news&condition=hot&device=android&page=%ld&per_page=20&status=0&tag_name=%@", self.page, self.myTitle];
    }
    if ([self.type isEqualToString:@"笑话段子"]) {
        self.nextPageAddress = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_album_list?category_name=entertainment&condition=hot&device=android&page=%ld&per_page=20&status=0&tag_name=%@", self.page, self.myTitle];
    }
    
    [NetHandler getDataWithUrl:self.nextPageAddress completion:^(id dict) {
        
        NSArray *arr = [dict objectForKey:@"list"];
        
        if (arr.count != 0) {
        for (NSDictionary *dic in arr) {
            FMAlbum *album = [[FMAlbum alloc] initWithDictionary:dic];
            [self.albumArr addObject:album];
            [album release];
        }
        [self.tableView reloadData];
        [self.tableView.footer endRefreshing];
        }
        else {
            self.tableView.footer.state = MJRefreshFooterStateNoMoreData;
        }
    }];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.albumArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AlbumTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"albumtableviewcellreuse" forIndexPath:indexPath];
    cell.album = [self.albumArr objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    AlbumViewController *albumVC = [[AlbumViewController alloc] init];
    FMAlbum *album = [self.albumArr objectAtIndex:indexPath.row];
    albumVC.address = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/1/15", album.albumID];
    albumVC.title11 = album.title;
    albumVC.album = album;
    albumVC.albumID = album.albumID;
    [self.navigationController pushViewController:albumVC animated:YES];
    [albumVC release];
}

- (void)leftButtonAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIButton *button = (UIButton *)[window viewWithTag:1000];
    button.hidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
